class LevelScene extends CoolScene
{
	public constructor() 
	{
		super();
		this.initBase();
	}
	private m_ContentArray:Array<Array<ContentPiece>>;
	protected m_TimeNode:TimeNode;
	protected m_StartNode:StartNode;
	protected m_OverNode:OverNode;
	protected m_BulletNode:BulletNode;
	protected m_backGround:egret.Bitmap;
	protected m_shield:egret.Bitmap;
	protected m_LevelData:Config_level;
	protected m_CreateTimeArray:Array<number>;
	protected m_LastTagArray:Array<number>
	private m_ContentBoards:Array<egret.DisplayObjectContainer>;
	private m_ScoreArray:Array<ScoreBase>;
	private initBase()
	{
		this.m_ScoreArray = new Array<ScoreBase>();
		this.m_LevelData = DataManager.getInstance().m_Config_json.levelData;
		console.log(this.m_LevelData);
		this.initCreateTimeArray();
		this.initBackGround();
		this.initTimeNode();
		this.initContent();
		this.initBulletNode();
		this.initStartNode();
		this.initOversNode();
	}
	private initBulletNode()
	{
		this.m_BulletNode = new BulletNode(this.bulletCallback, this);
		this.addChild(this.m_BulletNode);
	}
	private initCreateTimeArray()
	{
		this.m_CreateTimeArray = new Array<number>();
		this.m_LastTagArray = new Array<number>();
		for(let i = 0; i <this.m_LevelData.contents.length; i++)
		{
			this.m_CreateTimeArray.push(0);
			this.m_LastTagArray.push(-1);
		}
	}
	private initContent()
	{
		this.m_ContentBoards = new Array<egret.DisplayObjectContainer>();
		
		this.m_ContentArray = new Array<Array<ContentPiece>>();
		for(let i = 0; i < this.m_LevelData.contents.length; i++)
		{
			let contentPieceArr:Array<ContentPiece> = new Array<ContentPiece>();
			this.m_ContentArray.push(contentPieceArr);
			let contentBoard = new egret.DisplayObjectContainer();

			this.m_ContentBoards.push(contentBoard);
			this.addChild(contentBoard);
		}
	}

	public startUpdate()
	{
		//this.initContent();
		this.addEventListener(egret.Event.ENTER_FRAME, this.onUpdate, this);
	}
	public stopUpdate()
	{
		this.removeEventListener(egret.Event.ENTER_FRAME, this.onUpdate, this);
	}
	protected initBackGround()
	{
        this.m_backGround = this.createBitmapByName(this.m_LevelData.bgPic);
		this.m_backGround.anchorOffsetX = this.m_backGround.width / 2;
		this.m_backGround.anchorOffsetY = this.m_backGround.height / 2;
		this.m_backGround.x = this.width / 2;
		this.m_backGround.y = this.height / 2;
		this.addChild(this.m_backGround);
    }
	protected initShield()
	{
		let shield = this.createBitmapByName("blackbg");
		shield.alpha = 0;
		this.addChild(shield);
		shield.touchEnabled = true;//截取事件
		shield.visible = false;
		this.m_shield = shield;
	}
	protected initBgMusic()
	{
		if(!SoundEngine.getInstance().getMusicIsPlaying())
		{
			//let backMusicHash = DataManager.getInstance().getGameHttpData().m_backMusicHash;
			//SoundEngine.getInstance().playMusic(backMusicHash);
		}
	}
	protected timeEndCallback(event:egret.Event)
	{
		this.m_TimeNode.setTimerStop();
		this.stopUpdate();
		this.overNodeShow();
	}

	protected onUpdate(e:egret.Event)
	{
		this.onUpdateCreate();
		this.onUpdateMove();
	}
	private onUpdateCreate()
	{
		for(let i = 0; i < this.m_CreateTimeArray.length; i++)
		{
			this.m_CreateTimeArray[i]--;
			if(this.m_CreateTimeArray[i] <= 0)
			{
				let tag:number = Math.floor(Math.random() * this.m_LevelData.contents[i].moveContents.length);
				if(tag == this.m_LastTagArray[i])
				{
					if(tag == 0)
					{
						tag++;
					}
					else
					{
						tag--;
					}
				}
				// console.log(i + "---" + tag + "----" + this.m_LevelData.contents[i].moveContents.length);
				let content:Config_moveContent =  this.m_LevelData.contents[i].moveContents[tag];
				let contentPiece:ContentPiece = ContentPieceFactory.getInstance().CreateContentPiece(content.id,content.pic,content.speed,i);
				contentPiece.y = this.m_LevelData.contents[i].yPos;
				if(!contentPiece.m_isAdd)
				{
					contentPiece.m_isAdd = true;
					this.m_ContentBoards[i].addChild(contentPiece);
				}
				this.m_LastTagArray[i] = tag;
				this.m_CreateTimeArray[i] = 80;//Math.floor(Math.random() * 30) + 60;
				this.m_ContentArray[i].push(contentPiece);
			}
		}
	}
	private onUpdateMove()
	{
		for(let i = 0; i < this.m_ContentArray.length; i++)
		{
			for(let j = 0; j < this.m_ContentArray[i].length; j++)//this.m_ContentArray[i].length;
			{
				let xP = this.m_ContentArray[i][j].x + this.m_ContentArray[i][j].getSpeed();
				this.m_ContentArray[i][j].x = xP;
				if( xP > DataManager.getInstance().m_WindowsWidth + this.m_ContentArray[i][j].width / 2)
				{
					//this.stopUpdate();
					this.m_ContentArray[i][j].setDisable();
					this.m_ContentArray[i].splice(j, 1);
					j--;
				}
			}
		}
	}
	protected initTimeNode()
	{
		this.m_TimeNode = new TimeNode(this.timeEndCallback, this);
		this.m_TimeNode.setTime(this.m_LevelData.time);
		this.addChild(this.m_TimeNode);
	}
	private settingAgainCallback()
	{
		this.againGame();
	}
	// private settingNoticeCallback()
	// {	
	// 	this.m_SettingNode.visible = false;
	// 	this.m_NoticeNode.visible = true;
	// 	this.m_NoticeNode.playEffect();
	// }
	// private settingContinueCallback()
	// {
	// 	this.m_SettingNode.visible = false;
	// 	this.resumeGame();
	// }
	protected pauseGame()
	{
		this.m_TimeNode.setTimerStop();
	}
	protected resumeGame()
	{
		this.m_TimeNode.setTimerStart();
	}
	protected startGame()
	{
		this.startUpdate();
		this.m_TimeNode.setTimerStart();
	}
	/*
	重新开始当前关卡游戏
	*/
	protected againGame()
	{
		SoundEngine.getInstance().stopAllEffects();
		ContentPieceFactory.getInstance().Reset();
		SceneManager.getInstance().replaceScene(new LevelScene());
	}
	protected overOneLevel()
	{
		this.m_TimeNode.setTimerStop();
		SoundEngine.getInstance().stopAllEffects();
		SoundEngine.getInstance().playEffect("win_mp3",1);
		this.m_shield.visible = true;

		var timer:egret.Timer = new egret.Timer(2000,1);
        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.overNodeShow,this);
        timer.start();
	}

	protected overNodeShow()
	{
		for(let i = 0; i < this.m_ScoreArray.length; i++)
		{
			console.log(this.m_ScoreArray[i]);
		}
		this.m_BulletNode.stopAction();
		this.m_OverNode.visible = true;
		this.m_OverNode.setEndInfo(this.m_ScoreArray);
	}
	protected initStartNode()
	{
		let startNode = new StartNode();
		startNode.setCallback(this.startCallback, this);
		//startNode.visible = false;
		this.addChild(startNode);
		this.m_StartNode = startNode;
		this.m_StartNode.playEffect();
	}
	private startCallback()
	{
		this.m_StartNode.visible = false;
		this.m_StartNode.stopEffect();
		this.startGame();
	}

	// protected initNoticeNode()
	// {
	// 	let noticeNode = new NoticeNode();
	// 	noticeNode.setCallback(this.noticeAgainCallback, this.noticeContinueCallback,this);
	// 	noticeNode.visible = false;
	// 	this.addChild(noticeNode);
	// 	this.m_NoticeNode = noticeNode;
	// }
	// private noticeAgainCallback()
	// {
	// 	this.againGame();
	// }
	// private noticeContinueCallback()
	// {
	// 	this.m_NoticeNode.visible = false;
	// 	this.m_NoticeNode.stopEffect();
	// 	this.resumeGame();
	// }
	private overAgainCallback()
	{
		this.againGame();
	}
	private overNextCallback()
	{
		
	}
	protected initOversNode()
	{
		let overNode = new OverNode();
		overNode.visible = false;
		this.addChild(overNode);
		this.m_OverNode = overNode;
		this.m_OverNode.setCallback(this.overAgainCallback,this.overNextCallback,this);
	}

	private bulletCallback(stageX:number, stageY:number)
	{
		let isHit:boolean = false;
		let content:ContentPiece;
		for(let i = 0; i < this.m_ContentArray.length; i++)
		{
			for(let j = 0; j < this.m_ContentArray[i].length; j++)//this.m_ContentArray[i].length;
			{
				content = this.m_ContentArray[i][j];
				isHit = content.m_bitMap.hitTestPoint(stageX, stageY, true);//根据像素判断
				if(isHit)
				{
					SoundEngine.getInstance().playEffect("right_mp3",1);
					//content.setDisable();
					console.log("hit:" + content.getId() + "" + DataManager.getInstance().getNameById(content.getId()));
					this.m_ContentArray[i].splice(j, 1);
					let scoreBase = this.getScoreBase(content.getId());
					scoreBase.hitCount++;
					this.m_BulletNode.OnClicked(content);
					return;
				}
			}
		}
		this.m_BulletNode.OnUnClicked();
		SoundEngine.getInstance().playEffect("error_mp3",1);
	}
	private getScoreBase(id:string):ScoreBase
	{
		let score:ScoreBase;
		for(let i = 0; i < this.m_ScoreArray.length; i++)
		{
			if(this.m_ScoreArray[i].id == id)
			{
				score = this.m_ScoreArray[i];
				break;
			}
		}
		if(score == null)
		{
			score = new ScoreBase();
			score.id = id;
			this.m_ScoreArray.push(score);
		}
		return score;
	}
}